package com.wondream.myframework.juc;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wondream
 * @version 1.0
 * @create 2022/5/22 9:58 AM
 */
public class JUC01AtomicIntegerTest {

    @Test
    public void run() throws InterruptedException {
        final AtomicInteger atomicInteger = new AtomicInteger(10);
        boolean b = atomicInteger.compareAndSet(10, 2);
        System.out.println(b);
        System.out.println(atomicInteger.get());
        int intVal = atomicInteger.intValue();
        System.out.println(intVal);
        double doubleVal = atomicInteger.doubleValue();
        System.out.println(doubleVal);
        int intVal2 = atomicInteger.get();
        System.out.println(intVal2);
        int intVal3 = atomicInteger.incrementAndGet();
        System.out.println(intVal3);
        int intVal4 = atomicInteger.getAndAdd(-1);
        System.out.println(intVal4);
        System.out.println(atomicInteger.get());

        final int threadSize = 10;
        Thread[] ts = new Thread[10];
        for (int i = 0; i < threadSize; i++) {
            ts[i] = new Thread() {
                public void run(){
                    atomicInteger.addAndGet(10);
                }
            };
        }
        for (Thread t:
             ts) {
            t.start();
        }
        for (Thread t:
             ts) {
            t.join();
        }
        System.out.println(atomicInteger.get());
    }
}
